<template>
  <div id="app">
    <div class="login_bg">
      <div class="login_box">
        <!-- 头像 -->
        <div class="login_box_img">
          <img src="../assets/logo.png" alt />
        </div>
        <el-form
          ref="resetForm_all"
          :model="login_from"
          :rules="loginFormRules"
          class="login_box_form"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input v-model="login_from.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input v-model="login_from.password" prefix-icon="iconfont icon-3702mima"></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="btn_s">
            <el-button type="primary" @click="login_ok">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 登录表单的数据绑定对象
      login_from: {
        username: 'admin',
        password: '123456'
      },
      // 定义表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '长度在 6 到 12 个字符',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.resetForm_all.resetFields();
    },
    // 表单验证
    login_ok() {
      this.$refs.resetForm_all.validate(async (value) => {
        if (!value) return;
        const { data: res } = await this.$http.post('login', this.login_from);
        if (res.meta.status === 200) {
          // console.log(res.data.token)
          this.$Message.success('恭喜你，登录成功！');
          window.sessionStorage.setItem('token', res.data.token);
          console.log('将要发生跳转')
          this.$router.push('./Home');
        } else {
          this.$Message.error('不好意思，登录失败！');
        }
      });
    }
  }
};
</script>
<style lang='scss' scoped>
.login_bg {
  background-color: #2b4b6b;
  width: 100%;
  height: 100%;
  .login_box {
    width: 500px;
    height: 300px;
    background: white;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .login_box_img {
      width: 130px;
      height: 130px;
      border: 1px solid #eeeeee;
      border-radius: 50%;
      padding: 10px;
      background: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #eeeeee;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid #eeeeee;
      }
    }
    .login_box_form {
      width: 100%;
      position: absolute;
      bottom: 3px;
      padding: 0 30px;
      box-sizing: border-box;
      .btn_s {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
